package com.alibaba.csp.sentinel.dashboard.rule;

import com.alibaba.csp.sentinel.dashboard.domain.cluster.request.ClusterAppAssignMap;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author cyj
 **/
@Component("clusterAssignRuleNacosPublisher")
public class ClusterAssignRuleNacosPublisher implements DynamicRulePublisher<ClusterAppAssignMap>{

    @Resource
    private ConfigService configService;

    @Override
    public void publish(String app, ClusterAppAssignMap rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        configService.publishConfig(app + NacosConfigUtil.CLUSTER_MAP_DATA_ID_POSTFIX,
                NacosConfigUtil.GROUP_ID, JSON.toJSONString(rules));
    }
}
